package com.bicon.botu.finance.payment.remote;

import com.alibaba.fastjson.JSONObject;
import com.bicon.botu.finance.payment.alipay.AliPayApi;
import com.bicon.botu.finance.payment.common.PaymentConstants;
import com.bicon.botu.finance.payment.service.PayNotifyService;
import com.bicon.botu.finance.payment.weixin.utils.PayUtils;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * 支付通知
 *
 * @author tandk
 * @date 2017/9/12 0012 下午 16:02
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/static/remote/payNotify")
public class PayNotifyRemote {

    @Autowired
    private PayNotifyService payNotifyService;

    @Autowired
    private KafkaTemplate kafkaTemplate;


    /**
     * 支付宝异步通知接口
     *
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/alipay", method = RequestMethod.POST)
    public String alipay(HttpServletRequest request) throws IOException {
        //获取支付宝POST过来反馈信息
        Map<String, String> params = AliPayApi.toMap(request);
        //切记alipaypublickey是支付宝的公钥，请去open.alipay.com对应应用下查看。
        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        log.info("【支付异步通知返回参数】"+JSONObject.toJSONString(params));
        String result = payNotifyService.handlerAliPayResult(params);

        params.put("type", "AliPay");
        params.put("result", result);
        kafkaTemplate.send(params.get("passback_params"), JSONObject.toJSONString(params));
        return result;
    }


    /**
     * 微信支付异步通知接口
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/wxpay", method = RequestMethod.POST)
    public String wxpay(HttpServletRequest request, HttpServletResponse response) {
        log.info("【微信回调开始】");
        response.setContentType("text/html");
        BufferedReader in = null;
        String result = "";
        try {
            in = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        Map<String, String> resMp = PayUtils.decodeXml(result);
        String payResult = payNotifyService.wxpay(resMp, result);
        resMp.put("type", "WxPay");
        resMp.put("result", payResult);
        log.info("【微信回调结束：】" + resMp);
        log.info("【kafkaKey：】" + resMp.get("attach"));
        kafkaTemplate.send(resMp.get("attach"), JSONObject.toJSONString(resMp));
        return payResult;
    }


    /**
     * 银联支付异步通知接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/unionpay", method = RequestMethod.POST)
    public BaseResult unionpay(HttpServletRequest request)
            throws ServletException, IOException {
        boolean result = payNotifyService.handleUnionPaymentCallBack(request);
        if (result) {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_200, PaymentConstants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400);
        }
    }

    /**
     * 银联提现异步通知接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/unionTra", method = RequestMethod.POST)
    public BaseResult uniontraNotify(HttpServletRequest request)
            throws ServletException, IOException {
        boolean result = payNotifyService.handleUnionPaymentCallBack(request);
        if (result) {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_200, PaymentConstants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400);
        }
    }


}
